﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using ZL.Push.Web;


namespace ZL.Push
{
    public partial class SelectFriendWindowBase : BaseReadonlyWindow<tbl_friend>
    {
    }

    public partial class SelectFriendWindow : SelectFriendWindowBase
    {
        public SelectFriendWindow(RadTreeViewItem item)
        {
            InitializeComponent();
            base.InitializeComponent("tbl_friend");

            if (_search_control != null)
            {
                _search_control.Init(new string[] { "好友名称", "加入时间" },
                    new string[] { "nick_name", "join_datetime" },
                    new Type[] { typeof(string), typeof(DateTime?) });
            }

            App.s_copy_tree(ref _tree, item, null, CategoryType.eFriendCategory, -1);
            if (_tree.Items.Count > 0)
            {
                (_tree.Items.ElementAt(0) as RadTreeViewItem).IsExpanded = true; // 如果是根节点，展开第1层
            }

            search(null);
        }


        #region tree相关函数
        void _tree_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        {
            // 2013.1.5 add by long 如果是tree右键选中节点，会产生2次_tree_SelectionChanged
            //第一次e.RemovedItems.Count == 1, 第二次e.AddedItems.Count == 1，因此需要排除第一次
            if (e.AddedItems.Count == 0)
            {
                return;
            }
            if (_tree.SelectedItem == null)
            {
                return;
            }

            RadTreeViewItem tree_item = _tree.SelectedItem as RadTreeViewItem;
            TreeItemTag tag = tree_item.Tag as TreeItemTag;
            if (tag != null)
            {
               search(tag.parameter);
            }
            else
            {
               search(null);
            }
        }
        #endregion


        #region datagrid相关函数
        protected override void _datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            StackPanel sp = _datagrid.Columns[0].GetCellContent(e.Row) as StackPanel;
            TextBlock cell = sp.Children.ElementAt(0) as TextBlock;
            if (_datapager != null)
            {
                cell.Text = (_datapager.PageIndex * _datapager.PageSize + e.Row.GetIndex() + 1).ToString();
            }
            else
            {
                cell.Text = (e.Row.GetIndex() + 1).ToString();
            }
        }

        protected override void _datagrid_OnDoubleClicked(object sender, EventArgs e)
        {
            if (_datagrid.SelectedItem == null)
            {
                return;
            }

            FriendWindow win = new FriendWindow(AddOrEditType.eNone);
            win.DataContext = _datagrid.SelectedItem as tbl_friend;
            win.Show();
        }
        #endregion


        #region 操作函数
        private void _btn_ok_Click(object sender, RoutedEventArgs e)
        {
            if (_datagrid.SelectedItem == null)
            {
                MessageBox.Show("请选择好友");
                return;
            }

            this.DialogResult = true;
        }

        private void _btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        #endregion
    }
}

